package com.example.dell.intelligentagriculture;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dell.intelligentagriculture.Service.WebService;
import com.example.dell.intelligentagriculture.utils.Agriculture;
import com.example.dell.intelligentagriculture.utils.MyApp;

import org.json.JSONException;
import org.json.JSONObject;

public class SetupActivity extends AppCompatActivity {
    // co2
    TextView sco2txt, sco2txttai;
    EditText sco2txtmin, sco2txtmax;

    // 光照
    TextView sgtxt, sgtxttai;
    EditText sgtxtmin, sgtxtmax;

    // 空气温度
    TextView skwtxt, skwtxttai;
    EditText skwtxtmin, skwtxtmax;
    // 空气湿度
    TextView skstxt,skstxttai;
    EditText skstxtmin, skstxtmax;

    // 土壤温度
    TextView trwtxt, trwtxttai;
    EditText trwtxtmin, trwtxtmax;
    // 土壤湿度
    TextView trstxt, trstxttai;
    EditText trstxtmin, trstxtmax;

    LinearLayout homelay0, homelay1, homelay2, homelay3;
    MyApp myApp;
    private SetupActivity.MyReceiver myReceiver;
    SharedPreferences sp;
    public Agriculture agriculture;
    int minCo2,maxC02,minLight,maxLight,minSoilTemperature,maxSoilTemperature,minSoilHumidity,maxSoilHumidity,
            minAirTemperature,maxAirTemperature,minAirHumidity,maxAirHumidity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);
        initView();
        initReceiver();
        getData();
    }

    private void initView() {
        sp = getSharedPreferences("maxInfo", Context.MODE_PRIVATE);
        myApp= (MyApp) getApplication();
        // co2
        sco2txt = findViewById(R.id.sco2txt);
        sco2txttai = findViewById(R.id.sco2txttai);
        sco2txtmin = findViewById(R.id.sco2txtmin);
        sco2txtmax = findViewById(R.id.sco2txtmax);
     /*   sco2txt.setText(myApp.getCo2());
        sco2txtmin.setText(myApp.getMinCo2());
        sco2txtmax.setText(myApp.getMaxCo2());*/
        //sco2txtmin.setText();
        // 光照
        sgtxt = findViewById(R.id.sgtxt);
        sgtxttai = findViewById(R.id.sgtxttai);
        sgtxtmin = findViewById(R.id.sgtxtmin);
        sgtxtmax = findViewById(R.id.sgtxtmax);
        // 空气温度
        skwtxt = findViewById(R.id.skwtxt);
        skwtxttai = findViewById(R.id.skwtxttai);
        skwtxtmin = findViewById(R.id.skwtxtmin);
        skwtxtmax = findViewById(R.id.skwtxtmax);
        // 空气湿度
        skstxt = findViewById(R.id.skstxt);
        skstxttai = findViewById(R.id.skstxttai);
        skstxtmin = findViewById(R.id.skstxtmin);
        skstxtmax = findViewById(R.id.skstxtmax);
        // 土壤温度
        trwtxt = findViewById(R.id.trwtxt);
        trwtxttai = findViewById(R.id.trwtxttai);
        trwtxtmin = findViewById(R.id.trwtxtmin);
        trwtxtmax = findViewById(R.id.trwtxtmax);
        // 土壤湿度
        trstxt = findViewById(R.id.trstxt);
        trstxttai = findViewById(R.id.trstxttai);
        trstxtmin = findViewById(R.id.trstxtmin);
        trstxtmax = findViewById(R.id.trstxtmax);
        homelay0 = findViewById(R.id.homelay0);
        homelay1 = findViewById(R.id.homelay1);
        homelay2 = findViewById(R.id.homelay2);
        homelay3 = findViewById(R.id.homelay3);
        loadView();
    }

    /**
     * 动态架子页面布局
     */
    private void loadView() {
        Intent intent = getIntent();
        int item = intent.getIntExtra("ppounet", 1);
        switch (item) {
            case 10:
                homelay1.setVisibility(View.GONE);
                homelay2.setVisibility(View.GONE);
                homelay3.setVisibility(View.GONE);
                break;
            case 11:
                homelay0.setVisibility(View.GONE);
                homelay2.setVisibility(View.GONE);
                homelay3.setVisibility(View.GONE);
                break;
            case 12:
                homelay1.setVisibility(View.GONE);
                homelay0.setVisibility(View.GONE);
                homelay3.setVisibility(View.GONE);
                break;
            case 13:
                homelay1.setVisibility(View.GONE);
                homelay2.setVisibility(View.GONE);
                homelay0.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }
    /*
     * 加载最值
     * */
    public void getData(){
        String extreme = sp.getString("extreme", "");
        JSONObject json = null;
        try {
            json = new JSONObject(extreme);
            //二氧化碳最值
            minCo2 = json.getInt("minCo2");
            maxC02 = json.getInt("maxCo2");
            sco2txtmin.setText(minCo2+"");
            sco2txtmax.setText(maxC02+"");
            //光照最值
            minLight = json.getInt("minLight");
            maxLight = json.getInt("maxLight");
            sgtxtmin.setText(minLight+"");
            sgtxtmax.setText(maxLight+"");
            //土壤最值
            minSoilTemperature = json.getInt("minSoilTemperature");
            maxSoilTemperature = json.getInt("maxSoilTemperature");
            minSoilHumidity = json.getInt("minSoilHumidity");
            maxSoilHumidity = json.getInt("maxSoilHumidity");
            trwtxtmin.setText(minSoilTemperature+"");
            trwtxtmax.setText(maxSoilTemperature+"");
            trstxtmin.setText(minSoilHumidity+"");
            trstxtmax.setText(maxSoilHumidity+"");
            //空气最值
            minAirTemperature = json.getInt("minAirTemperature");
            maxAirTemperature = json.getInt("maxAirTemperature");
            minAirHumidity = json.getInt("minAirHumidity");
            maxAirHumidity = json.getInt("maxAirHumidity");
            skwtxtmin.setText(minAirTemperature+"");
            skwtxtmax.setText(maxAirTemperature+"");
            skstxtmin.setText(minAirHumidity+"");
            skstxtmax.setText(maxAirHumidity+"");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    /**
     * 注册广播接收者
     */
    private void initReceiver() {
        myReceiver = new SetupActivity.MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WebService.ACTION);
        registerReceiver(myReceiver, intentFilter);
    }

    /*
     * 广播接收者
     * */
    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (WebService.ACTION.equals(action)) {
                agriculture = (Agriculture) intent.getSerializableExtra("mySensor");
                sco2txt.setText(agriculture.getCo2()+"");
                sgtxt.setText(agriculture.getLight()+"");
                trwtxt.setText(agriculture.getSoilTemperature()+"");
                trstxt.setText(agriculture.getSoilHumidity()+"");
                skwtxt.setText(agriculture.getSoilTemperature()+"");
                skstxt.setText(agriculture.getSoilHumidity()+"");
                int co2 = agriculture.getCo2();
                int light = agriculture.getLight();
                int soilTemperature = agriculture.getSoilTemperature();
                int soilHumidity = agriculture.getSoilHumidity();
                int airTemperature = agriculture.getAirTemperature();
                int airHumidity = agriculture.getAirHumidity();
                if (co2 <= 600 && co2 >= 100){
                    sco2txttai.setText("正常");
                    sco2txttai.setTextColor(Color.parseColor("#0057ff"));
                }
                else {
                    sco2txttai.setText("异常");
                    sco2txttai.setTextColor(Color.parseColor("#ff0204"));
                }
                if (light <= 10000 && co2 >= 500){
                    sgtxttai.setText("正常");
                    sgtxttai.setTextColor(Color.parseColor("#0057ff"));
                }
                if (soilTemperature <= 90 && co2 >= 0){
                    trwtxttai.setText("正常");
                    trwtxttai.setTextColor(Color.parseColor("#0057ff"));
                }
                if (soilHumidity <= 70 && co2 >= 10){
                    trstxttai.setText("正常");
                    trstxttai.setTextColor(Color.parseColor("#0057ff"));
                }
                if (airTemperature <= 60 && co2 >= 0){
                    skwtxttai.setText("正常");
                    skwtxttai.setTextColor(Color.parseColor("#0057ff"));
                }
                if (airHumidity <= 70 && co2 >= 10){
                    skstxttai.setText("正常");
                    skstxttai.setTextColor(Color.parseColor("#0057ff"));
                }

            }
        }
    }

}

